<?php
namespace Modules\Admin\Controllers;

use Modules\Admin\Model\Data\User as DataUser;
use Base\Exception\Controller as Exception;
use \S\Request;

/**
 * @name 用户组管理
 */
class Group extends Controller {

    protected $sys_view = true;

    /**
     * @name 用户组管理
     */
    public function indexAction() {
        $data_group = new DataUser();
        $this->response['group'] = $data_group->getGroupList();
    }

    /**
     * @name 编辑组(弹窗)
     *
     * @return void
     */
    public function editPopAction() {
        $data_user = new DataUser();
        $id = intval(trim($this->getParams('id')));
        $params = array();
        if($id) {
            $params = $data_user->getGroupOneById($id);
        }

        $this->setResponseFormat(\S\Response::FORMAT_PLAIN);
        $this->response = $this->getRenderView("", $params);
    }

    /**
     * @name 用户成员列表
     */
    public function userListAction() {
        $gid = intval($this->getParams('gid'));
        $data_user = new DataUser();
        $this->response['group'] = $data_user->getGroupOneById($gid);
        $this->response['member'] = $data_user->getUserByGid($gid);
    }

    /**
     * @name 删除用户组
     * @throws Exception
     */
    public function delAction() {
        $gid = intval($this->getParams('gid'));
        (new DataUser())->delGroupOneById($gid);
    }

    /**
     * @name 用户所属组配置
     * @throws Exception
     */
    public function userGroupAction() {
        $uid = intval($this->getParams('uid'));
        if(!$uid) {
            throw new Exception('error.admin.user_id_does_not_exist');
        }
        $data_user = new DataUser();

        $this->response['user'] = $data_user->getUserInfoById($uid);
        $this->response['group'] = $data_user->getGroupByUid($uid);
        $this->response['groups'] = $data_user->getGroupList();
    }

    /**
     * @name 用户所属组保存操作
     * @throws Exception
     */
    public function saveUserGroupAction() {
        $gids = Request::post('gids');
        $uid = (int)Request::post('uid');

        if(!$uid) {
            throw new Exception('error.admin.user_does_not_exist');
        }

        $data_user = new DataUser();
        $user = $data_user->getUserInfoById($uid);
        if(!$user) {
            throw new Exception('error.admin.user_does_not_exist');
        }

        if($gids) {
            $group = $data_user->getGroupByUid($uid);
            // 添加
            $add = array_diff($gids, $group);
            if ($add) {
                foreach ($add as $ag) {
                    $data_user->addUserGroupRel($ag, $uid);
                }
            }
            // 删除
            $del = array_diff($group, $gids);
            if ($del) {
                foreach ($del as $dg) {
                    $data_user->delUserGroupRel($dg, $uid);
                }
            }
        } else {
            $data_user->delGroupRelById($uid);
        }
    }

    /**
     * @name 检查用户组是否存在
     * @throws Exception
     */
    public function existAction() {
        $name = $this->getParams('gname');
        if(!$name || addslashes($name) !== $name) {
            throw new Exception('error.admin.user_group_name_error');
        }

        $data_user = new DataUser();
        if($data_user->isExistsByGroupName($name)) {
            throw new Exception('error.admin.user_group_exist');
        }
    }

    /**
     * @name 保存组信息
     * @throws Exception
     */
    public function saveGroupAction() {
        $gid = intval($this->getParams('gid'));
        $name = trim($this->getParams('gname'));
        $description = htmlspecialchars(trim($this->getParams('description')));

        if(!$name || $name != addslashes($name)) {
            throw new Exception('error.admin.user_group_name_error');
        }

        $data_user = new DataUser();
        if($data_user->isExistsByGroupName($name)) {
            throw new Exception('error.admin.user_group_exist');
        }
        $data_user->saveGroupInfo(array('gname'=>$name, 'description'=>$description, 'ctime'=>date('Y-m-d H:i:s')), $gid);
    }
}
